public class Solution1802 {
    public int maxValue(int n, int index, int maxSum) {
        int l=1,r=maxSum,ans=-1;
        while (l<=r){
            long mid=(l+r)/2;
            long count=0;
            if (mid>index){
                count+=(2*mid-index)*(index+1)/2;
            }else {
                count+=(mid+1)*mid/2+index+1-mid;
            }
            if (mid-n+1+index>0){
                count+=(2*mid-n+index)*(n-index-1)/2;
            }else {
                count+=(mid-1)*mid/2+n-index-mid;
            }
            if (count<=maxSum){
                ans=(int)mid;
                l=(int)mid+1;
            }else {
                r=(int)mid-1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1802().maxValue(6,2,931384943));
    }
}
